<?php
defined('BASEPATH') OR exit('No direct script access allowed');
 
class Coach extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	 var $dbmain;
     function __construct()
	 {
		    parent::__construct();
            include_once "func.php";
            $this->dbmain = $db;
            $this->load->model('CoachModel');
 	 } 

 	

	public function index()
	{
	 	$param = array();
	 	$coachid = $_GET["id"];
	 	$param['id'] = $coachid;
		$res = $this->CoachModel->getCoach($param);

 		$res->stypename = $this->CoachModel->getStypename($res->stype_main);
 		$res->headbg = $this->CoachModel->getStypebg($res->stype_main);

        $data = array(
			'coachinfo' => $res,
			'stype' => 'all'
		);



		$this->load->view('coach',$data);
	}

    public function register(){

        //目前member暂时只有coach
        $member_id = '0';
        if(!isset($_COOKIE['member_id'])){
              
 		}else{
 			if($_COOKIE['member_id']!=''){
 			    $member_id = $_COOKIE['member_id'];
 			}
  		}

        $data['member_id'] = $member_id;


 		$this->load->view('web_coach_register',$data);
    }

    public function register_handle(){

        $phonenumber = $_POST['phonenumber']; 
        $checkcode = $_POST['checkcode']; 
        $realname = $_POST['realname']; 
	    $password = md5($_POST['password']); 

		if($phonenumber==''||$checkcode==''||$realname==''||$password==''){
			$return = array(
			    'result' => 3
			);
			ajax_return($return);
		}

 		if(!preg_match("/1[3458]{1}\d{9}$/",$phonenumber)){  
			$return = array(
			    'result' => 4
			);
			ajax_return($return);
		 }

		 //check code
 
 


	    //insert into member
	    $sql = "insert into member(nickname,password,phonenumber,createtime) values('$realname','$password','$phonenumber',NOW())";
	    $res = $this->dbmain->query($sql);
	    $member_id = $this->dbmain->insert_id;
  
	    //insert into coach
	    $sql = "insert into coach(fullname,member_id) values('$realname','$member_id')";
	    $res = $this->dbmain->query($sql);

        //写入cookie
        setcookie('member_id',$member_id,time()+3600*24*365,'/');

		$return = array(
			    'result' => 1,
			    'member_id' => $member_id
		);
		ajax_return($return);

    }


	public function reg(){
        
 		$this->connectWX('snsapi_base'); 
 
		// //获取openid
		// $openid = '';
		// //去获取openid 
 
		// if(isset($_GET['openid'])){
		//     $openid = $_GET['openid'];
		// }else{
  // 			$base_uri = urlencode('http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]);
		//     $backurl = $base_uri.'?backfrom=indexgetopenid';
		//     $this->connectWX($backurl,'snsapi_base'); 
		// }

 	// 	//如果有wx_openid 直接获取member表的信息
		// $sql = "select * from member where wx_openid='$openid'";
		// $member = $this->dbmain->get_row($sql);
 
		// //如果没有 跳转到注册页面
 

		// if(!$member){
		// 	 //$this->load->view('login',$data);

		// 	 echo("1111");

		// 	 header("Location: /index.php/profile/login");
		// 	 exit;

		// }else{

		// 	 echo("222222");

 	//         $data = array(
	 //        	'member' => $member,
	 // 			'stype' => 'all'
		// 	);

	 //        //判断是否到教练注册页面 还是到管理入口页面
		// 	$ifCoach = true;

	 //        if($ifCoach){
		// 		 header("Location: /index.php/coach/admin_dashboard");
		// 		 exit;
	 //        }
		// }


        //判断是否到手机注册页面



 	}

	public function admin_getwxinfo(){
		$openid = $_GET['openid'];

		//如果有wx_openid 直接获取member表的信息
		$sql = "select * from member where wx_openid='$openid'";
		$member = $this->dbmain->get_row($sql);

		///echo($sql);

 
		if(!$member){
			 //$this->load->view('login',$data);
			 header("Location: /index.php/coach/login");
			 exit;

		}else{

			$from = $_GET['from'];

 	        $data = array(
	        	'member' => $member,
	 			'stype' => 'all',
	 			'from' => $from
			);

			$this->load->view('coach_admin_dashboard',$data);
		}

	}

	public function admin_welcome(){

		//setcookie('loginfrom','coach',time()+3600);
	    //header("location: http://www.xunlianri.com/index.php/profile");
 		$this->connectWX('snsapi_base'); 

	    //$this->connectWX('snsapi_base'); 

		//获取openid 判断是否是教练
		// $openid = '';
		// //去获取openid 
		// if(isset($_GET['openid'])){
		//     $openid = $_GET['openid'];
		// }else{
		// 	echo("正在与微信连接...");
		// 	// $base_uri = urlencode('http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]);
		//  //    $backurl = $base_uri.'?backfrom=indexgetopenid';
		//     $this->connectWX('snsapi_base'); 
		// }
 
	}

 	public function weblogin(){

 		$member_id = '0';
        if(!isset($_COOKIE['member_id'])){
              
 		}else{
 			if($_COOKIE['member_id']!=''){
 			    $member_id = $_COOKIE['member_id'];
 			}
  		}

        $data['member_id'] = $member_id; 

 		$this->load->view('web_coach_login',$data);
 	}

 	public function login(){
 		$this->load->view('coachlogin');
 	}

  	public function logout_handle(){
        setcookie('member_id','0',time()-3600*24*365,'/');
        //setcookie("member_id");

        header("Location: /");
  	}

  	public function login_handle(){
        
        $phonenumber = $_POST['phonenumber'];
        $password = md5($_POST['password']);

        $sql = "select * from member where phonenumber='$phonenumber' and password='$password'";
  		$member = $this->dbmain->get_row($sql);
  		if(!$member){
 			$return = array(
				    'result' => 7
			);
			ajax_return($return);
   		}

        //获得member_id
        $member_id = $member->id;
  
        //写入cookie
        setcookie('member_id',$member_id,time()+3600*24*365,'/');

		$return = array(
			    'result' => 1,
			    'member_id' => $member_id
		);
		ajax_return($return);
  	}


	function connectWX($scopetype){
	    $redirect_uri = urlencode("http://www.xunlianri.com/wxoauth2/auth.php?from=coach");
	    $oauth2_uri = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx8ed1756092833c6a&redirect_uri=$redirect_uri&response_type=code&scope=".$scopetype."&state=1#wechat_redirect";
	    header("location: $oauth2_uri");
	    exit;
	}



  	public function getCoachInfoById($member_id){
		 //根据memberid判断是教练
		 $sql = "select * from coach where member_id = $member_id";
  		 $coach = $this->dbmain->get_row($sql);

		 $ifCoachReg = false;
		 $reviewcode = '0|0|0';

 		 if($coach){
		 	$ifCoachReg =  true;
		 	if($coach->thumb == ''){
		 		$coach->thumb = '/images/icon/user.png';
		 	}
		 	$reviewcode = $coach->review;

 		 }else{
 		 	$coach = new stdClass();
		    $coach->thumb = '/images/icon/user.png';
		 }

		 //获取认证状态
		 $reviewarr = explode('|',$reviewcode);
  
 	     $data = array(
 	     	 'ifCoachReg' => $ifCoachReg,
	         'coach' => $coach,
 	         'review_step' => $reviewarr
		 );

		 return $data;


  	}


    //获取教练信息
 	public function getCoachInfo($wx_openid){

         //首先获取对应的member_id
 	     $sql = "select * from member where wx_openid = '$wx_openid'";
		 $member = $this->dbmain->get_row($sql);
 		 $phonenumber = $member->phonenumber;
		 $member_id = $member->id;

 
		 //根据memberid判断是教练
		 $sql = "select * from coach where member_id = $member_id";
 		 $coach = $this->dbmain->get_row($sql);

 		 //如果不是教练 再判断手机号
		 if(!$coach){
		 		//判断是否有phonenumber
				$sql = "select * from coach where phonenumber = '$phonenumber'";
				$coach = $this->dbmain->get_row($sql);
		 }

		 $ifCoachReg = false;
		 $reviewcode = '0|0|0';

 		 if($coach){
		 	$ifCoachReg =  true;
		 	//print_r($coach);
		 	if($coach->thumb == ''){
		 		$coach->thumb = '/images/icon/user.png';
		 	}
		 	$reviewcode = $coach->review;

 		 }else{
 		 	$coach = new stdClass();
		    $coach->thumb = '/images/icon/user.png';
		 	//echo("not coach");
		 }

		 //获取认证状态
		 $reviewarr = explode('|',$reviewcode);
  
 	     $data = array(
 	     	 'ifCoachReg' => $ifCoachReg,
	         'coach' => $coach,
	         'wx_openid' => $wx_openid,
	         'review_step' => $reviewarr
		 );

		 return $data;
	}


    //进入web管理
	public function web_dashboard(){

		if(!isset($_COOKIE['member_id'])){
			 echo('未登录！<a href=/index.php/coach/weblogin>点击跳转</a>');
			 // header("Location: /index.php/coach/weblogin");
			 exit;
		}

 		$member_id = $_COOKIE['member_id'];

  	    $data =  $this->getCoachInfoById($member_id);

  	    if(!$data['ifCoachReg']){
  	     	exit;
  	    }

		$this->load->view('web_coach_dashboard',$data);
	} 


	public function web_dashboard_baseinfo(){

		if(!isset($_COOKIE['member_id'])){
			 echo('未登录！<a href=/index.php/coach/weblogin>点击跳转</a>');
			 exit;
		}

 		 $member_id = $_COOKIE['member_id'];

  	     $data =  $this->getCoachInfoById($member_id);

  	     if(!$data['ifCoachReg']){
  	     	exit;
  	     }

		 $this->load->view('web_dashboard_baseinfo',$data);
	} 

    

    public function web_dashboard_baseinfo_handle(){

		if(!isset($_COOKIE['member_id'])){
			 echo('未登录！<a href=/index.php/coach/weblogin>点击跳转</a>');
			 exit;
		}

 		 $member_id = $_COOKIE['member_id'];

  	     $data =  $this->getCoachInfoById($member_id);

  	     if(!$data['ifCoachReg']){
  	     	exit;
  	     }

		//从member表中获取数据
 		$sql = "select * from member where id='$member_id'";
		$member = $this->dbmain->get_row($sql);
		$phonenumber = '';
		//$member_id = '';
		if($member){
			$phonenumber = $member->phonenumber;
			$member_id = $member->id;
		}

		//获取修改的参数
 		$fullname = $_POST['fullname'];
		$qqnumber = $_POST['qqnumber'];
		$sex = $_POST['sex'];
		$height = $_POST['height'];
		$weight = $_POST['weight'];
		$birthday = $_POST['birthday'];
		$thumb = $_POST['thumb'];
		$description = $_POST['selfintro'];
		$resume = $_POST['resume'];
		$sporttitle = $_POST['sporttitle'];
		$stype_main = $_POST['stype_main'];
		$orthertype = $_POST['orthertype'];

		if($fullname==''){
			$return = array(
			    'result' => 3
			);
			ajax_return($return);
		}

		//修改或插入coach表
		$sql = "select * from coach where member_id=$member_id";
 		$checksql = $this->dbmain->get_row($sql);

		if($checksql){
			$reviewcode = $checksql->review;
			$reviewarr = explode('|',$reviewcode);
			$reviewarr[0] = '1';
			$reviewcode = implode('|',$reviewarr);

		    $sql = "update coach set fullname='$fullname',height='$height',weight='$weight',birthday='$birthday',qqnumber='$qqnumber',sex='$sex',thumb='$thumb',description='$description',review='$reviewcode',resume='$resume',sporttitle='$sporttitle',stype_main='$stype_main',orthertype='$orthertype' where member_id=$member_id";
		    $this->dbmain->query($sql);
		}else{
			$sql = "insert into coach(fullname,height,weight,birthday,phonenumber,qqnumber,sex,createdate,member_id,thumb,description,resume,sporttitle,stype_main,orthertype) 
			values('$fullname','$height','$weight','$birthday','$phonenumber','$qqnumber','$sex',NOW(),$member_id,'$thumb','$description','$resume','$sporttitle','$stype_main','$orthertype')";
			$res = $this->dbmain->query($sql);
		}

	  	$return = array(
		    'result' => 1
		);
		ajax_return($return);


 
	 }


	public function web_dashboard_video(){

		if(!isset($_COOKIE['member_id'])){
			 echo('未登录！<a href=/index.php/coach/weblogin>点击跳转</a>');
			 exit;
		}

 		 $member_id = $_COOKIE['member_id'];
  	     $data =  $this->getCoachInfoById($member_id);

  	     if(!$data['ifCoachReg']){
  	     	exit;
  	     }

  	     $sql = "select * from member_video where member_id='$member_id' order by id desc";
 	 	 $ablum = $this->dbmain->get_results($sql);
 	 	 $data['video_list'] = $ablum; 

		 $this->load->view('web_dashboard_video',$data);
	} 


	public function web_dashboard_preview(){

		if(!isset($_COOKIE['member_id'])){
			 echo('未登录！<a href=/index.php/coach/weblogin>点击跳转</a>');
			 exit;
		}

 		 $member_id = $_COOKIE['member_id'];
  	     $data =  $this->getCoachInfoById($member_id);

  	     if(!$data['ifCoachReg']){
  	     	exit;
  	     }

 
		 $this->load->view('web_dashboard_preview',$data);
	} 


	public function web_dashboard_ablum(){

		if(!isset($_COOKIE['member_id'])){
			 echo('未登录！<a href=/index.php/coach/weblogin>点击跳转</a>');
			 exit;
		}

 		 $member_id = $_COOKIE['member_id'];
  	     $data =  $this->getCoachInfoById($member_id);

  	     if(!$data['ifCoachReg']){
  	     	exit;
  	     }

  	     $sql = "select * from member_ablum where member_id='$member_id' order by id desc";
 	 	 $ablum = $this->dbmain->get_results($sql);
 	 	 $data['ablum'] = $ablum; 

		 $this->load->view('web_dashboard_ablum',$data);
	} 




   public function web_dashboard_video_handle(){

		if(!isset($_COOKIE['member_id'])){
			 echo('未登录！<a href=/index.php/coach/weblogin>点击跳转</a>');
			 exit;
		}
 		$member_id = $_COOKIE['member_id'];

 		$video_src = $_POST['video_src'];
  		$video_desc = $_POST['video_desc'];
 		$sql = "insert into member_video(video_src,video_desc,member_id) values('$video_src','$video_desc','$member_id')";
 		$this->dbmain->query($sql);

  	   	$return = array(
		    'result' => 1
		);
		ajax_return($return);
	} 


	public function web_dashboard_ablum_handle(){

		if(!isset($_COOKIE['member_id'])){
			 echo('未登录！<a href=/index.php/coach/weblogin>点击跳转</a>');
			 exit;
		}
 		$member_id = $_COOKIE['member_id'];

 		$img_src = $_POST['img_src'];
 		$thumb_src = $_POST['thumb_src'];
 		$img_desc = $_POST['img_desc'];
 		$sql = "insert into member_ablum(thumb_src,img_src,img_desc,member_id) values('$thumb_src','$img_src','$img_desc','$member_id')";
 		$this->dbmain->query($sql);

  	   	$return = array(
		    'result' => 1
		);
		ajax_return($return);
	} 


	public function web_dashboard_cal(){

		if(!isset($_COOKIE['member_id'])){
			 echo('未登录！<a href=/index.php/coach/weblogin>点击跳转</a>');
			 exit;
		}

 		 $member_id = $_COOKIE['member_id'];

  	     $data =  $this->getCoachInfoById($member_id);

  	     if(!$data['ifCoachReg']){
  	     	exit;
  	     }

		 $this->load->view('web_dashboard_cal',$data);
	} 


	public function web_dashboard_certinfo(){

		if(!isset($_COOKIE['member_id'])){
			 echo('未登录！<a href=/index.php/coach/weblogin>点击跳转</a>');
			 exit;
		}

 		 $member_id = $_COOKIE['member_id'];

  	     $data =  $this->getCoachInfoById($member_id);

  	     if(!$data['ifCoachReg']){
  	     	exit;
  	     }

		 $this->load->view('web_dashboard_certinfo',$data);
	} 


	public function web_dashboard_certinfo_handle(){


		if(!isset($_COOKIE['member_id'])){
			 echo('未登录！<a href=/index.php/coach/weblogin>点击跳转</a>');
			 exit;
		}

 		 $member_id = $_COOKIE['member_id'];

  	     $data =  $this->getCoachInfoById($member_id);

  	     if(!$data['ifCoachReg']){
  	     	exit;
  	     }

		//从member表中获取数据
 		$sql = "select * from member where id='$member_id'";
		$member = $this->dbmain->get_row($sql);
		$phonenumber = '';
		//$member_id = '';
		if($member){
			$phonenumber = $member->phonenumber;
			$member_id = $member->id;
		}

		$idcard = $_POST['idcard'];
		$cert1 =  $_POST['cert1'];
 

		//修改或插入coach表
		$sql = "select * from coach where member_id=$member_id";
		$checksql = $this->dbmain->get_row($sql);

		if($checksql){
			$reviewcode = $checksql->review;
			$reviewarr = explode('|',$reviewcode);
			$reviewarr[1] = '1';
			$reviewcode = implode('|',$reviewarr);
			$sql = "update coach set idcard='$idcard',cert1='$cert1',review='$reviewcode' where member_id=$member_id";
		    $res = $this->dbmain->query($sql);
		}else{
		  	$return = array(
			    'result' => 7
			);
			ajax_return($return);
		}


	  	$return = array(
		    'result' => 1
		);
		ajax_return($return);

	}


	public function web_dashboard_courseinfo_edit(){

		if(!isset($_COOKIE['member_id'])){
			 echo('未登录！<a href=/index.php/coach/weblogin>点击跳转</a>');
			 exit;
		}

 		 $member_id = $_COOKIE['member_id'];
  	     $data =  $this->getCoachInfoById($member_id);


 		 $cid = $_GET['cid'];
 		 $sql = "select * from c_course where id='$cid'";
 		 $c_course = $this->dbmain->get_row($sql);

 
		$data['c_course'] = $c_course;

		 $this->load->view('web_dashboard_courseinfo_edit',$data);
	}


 

	public function web_dashboard_courseinfo(){

		if(!isset($_COOKIE['member_id'])){
			 echo('未登录！<a href=/index.php/coach/weblogin>点击跳转</a>');
			 exit;
		}

 		 $member_id = $_COOKIE['member_id'];

  	     $data =  $this->getCoachInfoById($member_id);

  	     if(!$data['ifCoachReg']){
  	     	exit;
  	     }

  	    $sql = "select * from c_course where member_id=$member_id";
        $c_course_list = $this->dbmain->get_results($sql);
 
  //       $data = array(
		// 	'coach' => $coachdata,
		// 	'c_course_list' => $c_course_list
		// );

		$data['c_course_list'] = $c_course_list;


		$this->load->view('web_dashboard_courseinfo',$data);
	} 
 
 
 	public function web_dashboard_courseinfo_del_handle(){

		if(!isset($_COOKIE['member_id'])){
			 echo('未登录！<a href=/index.php/coach/weblogin>点击跳转</a>');
			 exit;
		}

 		$member_id = $_COOKIE['member_id'];

 		$cid = $_POST['cid'];
 		$sql = "delete from c_course where id='$cid' and member_id='$member_id'";
 		$this->dbmain->query($sql);

	  	$return = array(
		    'result' => 1
		);
		ajax_return($return);

 	}


    public function web_dashboard_cal_handle(){
		if(!isset($_COOKIE['member_id'])){
			 echo('未登录！<a href=/index.php/coach/weblogin>点击跳转</a>');
			 exit;
		}

 		$member_id = $_COOKIE['member_id'];
 		$checkval = $_POST["checkval"];
     	$caldesc = $_POST["caldesc"];


 		$sql = "update coach set calval='$checkval',caldesc='$caldesc' where member_id='$member_id'";
 		$this->dbmain->query($sql);

	  	$return = array(
		    'result' => 1
		);
		ajax_return($return);

    }


	public function web_dashboard_courseinfo_handle(){
         
		if(!isset($_COOKIE['member_id'])){
			 echo('未登录！<a href=/index.php/coach/weblogin>点击跳转</a>');
			 exit;
		}

 		$member_id = $_COOKIE['member_id'];

		//检查member
 		$sql = "select * from member where id='$member_id'";
		$member = $this->dbmain->get_row($sql);
		$phonenumber = '';
		//$member_id = '';
		if($member){
			$phonenumber = $member->phonenumber;
			$member_id = $member->id;
		}else{
			exit;
		}
 

         //检查coach
  	     $coachdata =  $this->getCoachInfoById($member_id);
  	     if(!$coachdata['ifCoachReg']){
				$return = array(
				    'result' => 9
				);
				ajax_return($return);
  	     }

          
        //获取参数
  		$course_id = $_POST['course_id'];
  		$course_name = $_POST['course_name'];
 		$course_desc = $_POST['course_desc'];
		$price = $_POST['price'];
		$othertype = '';
		$stype = '';

		if(!is_numeric($price)){
			$return = array(
			    'result' => 6
			);
			ajax_return($return);
		}


	    //更新review状态
	    $reviewcode = $coachdata['coach']->review;
	    $reviewarr = explode('|',$reviewcode);
		$reviewarr[2] = '1';
		$reviewcode = implode('|',$reviewarr);
 

 		//修改或插入c_course表
 
		if($course_id!='0'){
			
  	        $sql = "update coach set review='$reviewcode'  where member_id=$member_id";
		    $res = $this->dbmain->query($sql);

		    //更新c_course表
            $sql = "update c_course set othertype='$othertype',stype='$stype',course_name='$course_name',course_desc='$course_desc',price='$price' where id=$course_id";
		    $res = $this->dbmain->query($sql);

		}else{
		   $sql = "insert into c_course(othertype,stype,course_name,course_desc,price,member_id) values('$othertype','$stype','$course_name','$course_desc',$price,$member_id)";	 
		   $res = $this->dbmain->query($sql);

  	        $sql = "update coach set review='$reviewcode' where member_id=$member_id";
		    $res = $this->dbmain->query($sql);
 		}


	  	$return = array(
		    'result' => 1
		);
		ajax_return($return);



   }


	public function admin_dashboard_courseinfo(){


 		 if(!isset($_GET['wx_openid'])){
		 	 exit;
		 }

		 $wx_openid = $_GET['wx_openid'];

 	     $data =  $this->getCoachInfo($wx_openid);

 	     $member_id = $data['coach']->member_id;

 	     $sql = "select * from c_course where member_id=$member_id";
         $c_course = $this->dbmain->get_row($sql);

         $data['c_course'] = $c_course;

		 $this->load->view('coach_admin_dashboard_courseinfo',$data);

	}


 

	
	public function admin_dashboard_certinfo(){


 		 if(!isset($_GET['wx_openid'])){
		 	 exit;
		 }

		 $wx_openid = $_GET['wx_openid'];

 	     $data =  $this->getCoachInfo($wx_openid);

 	     $idcard = $data['coach']->idcard;
 	     $cert1 = $data['coach']->cert1;
 	     if($idcard==''){
 	     	$data['coach']->idcard = '/images/addimg.png';
 	     }
 	     if($cert1==''){
 	     	$data['coach']->cert1 = '/images/addimg.png';
 	     }
		 $this->load->view('coach_admin_dashboard_certinfo',$data);
	}




    //进入管理
	public function admin_dashboard(){

 		 if(!isset($_GET['wx_openid'])){
		 	 exit;
		 }

		 $wx_openid = $_GET['wx_openid'];

 	     $data =  $this->getCoachInfo($wx_openid);
 
		 $this->load->view('coach_admin_dashboard',$data);
	} 


	public function admin_dashboard_baseinfo(){
 		 if(!isset($_GET['wx_openid'])){
		 	 exit;
		 }
		 $wx_openid = $_GET['wx_openid'];

 	     $data =  $this->getCoachInfo($wx_openid);
 
		 $this->load->view('coach_admin_dashboard_baseinfo',$data);
	}



	public function admin_couseinfo_handle(){
		//获取wx_openid
		$wx_openid = $_POST['wx_openid'];
		if(!$wx_openid){
			$return = array(
			    'result' => 7
			);
			ajax_return($return);
		}

		//从member表中获取数据
 		$sql = "select * from member where wx_openid='$wx_openid'";
		$member = $this->dbmain->get_row($sql);
		$phonenumber = '';
		$member_id = '';
		if($member){
 			$member_id = $member->id;
		}

		$othertype = $_POST['othertype'];
		$stype =  $_POST['stype'];
		$course_desc = $_POST['course_desc'];
		$price = $_POST['price'];
		if(!is_numeric($price)){
			$return = array(
			    'result' => 6
			);
			ajax_return($return);
		}

		//检查member
		$sql = "select * from coach where member_id=$member_id";
		$member = $this->dbmain->get_row($sql);
		if(!$member){
			$return = array(
			    'result' => 9
			);
			ajax_return($return);
		}
	    //更新review状态
	    $reviewcode = $member->review;
	    $reviewarr = explode('|',$reviewcode);
		$reviewarr[2] = '1';
		$reviewcode = implode('|',$reviewarr);


 		//修改或插入c_course表
		$sql = "select * from c_course where member_id=$member_id";
		$checksql = $this->dbmain->get_row($sql);

		if($checksql){
			
  	        $sql = "update coach set review='$reviewcode',stype_main='$stype',stype_main_price='$price' where member_id=$member_id";
		    $res = $this->dbmain->query($sql);

		    //更新c_course表
            $sql = "update c_course set othertype='$othertype',stype='$stype',course_desc='$course_desc',price='$price' where member_id=$member_id";
		    $res = $this->dbmain->query($sql);

		}else{
		   $sql = "insert into c_course(othertype,stype,course_desc,price,member_id) values('$othertype','$stype','$course_desc',$price,$member_id)";	 
		   $res = $this->dbmain->query($sql);

  	        $sql = "update coach set review='$reviewcode',stype_main='$stype',stype_main_price='$price' where member_id=$member_id";
		    $res = $this->dbmain->query($sql);
 		}


	  	$return = array(
		    'result' => 1
		);
		ajax_return($return);

	}

	public function admin_certinfo_handle(){

		//获取wx_openid
		$wx_openid = $_POST['wx_openid'];
		if(!$wx_openid){
			$return = array(
			    'result' => 7
			);
			ajax_return($return);
		}

		//从member表中获取数据
 		$sql = "select * from member where wx_openid='$wx_openid'";
		$member = $this->dbmain->get_row($sql);
		$phonenumber = '';
		$member_id = '';
		if($member){
			$phonenumber = $member->phonenumber;
			$member_id = $member->id;
		}

		$idcard = $_POST['idcard'];
		$cert1 =  $_POST['cert1'];
 

		//修改或插入coach表
		$sql = "select * from coach where member_id=$member_id";
		$checksql = $this->dbmain->get_row($sql);

		if($checksql){
			$reviewcode = $checksql->review;
			$reviewarr = explode('|',$reviewcode);
			$reviewarr[1] = '1';
			$reviewcode = implode('|',$reviewarr);
			$sql = "update coach set idcard='$idcard',cert1='$cert1',review='$reviewcode' where member_id=$member_id";
		    $res = $this->dbmain->query($sql);
		}else{
		  	$return = array(
			    'result' => 7
			);
			ajax_return($return);
		}


	  	$return = array(
		    'result' => 1
		);
		ajax_return($return);

	}


	public function admin_baseinfo_handle(){

		//获取wx_openid
		$wx_openid = $_POST['wx_openid'];
		 if(!$wx_openid){
			$return = array(
			    'result' => 7
			);
			ajax_return($return);
		}


		//从member表中获取数据
 		$sql = "select * from member where wx_openid='$wx_openid'";
		$member = $this->dbmain->get_row($sql);
		$phonenumber = '';
		$member_id = '';
		if($member){
			$phonenumber = $member->phonenumber;
			$member_id = $member->id;
		}

		//获取修改的参数
 		$fullname = $_POST['fullname'];
		$qqnumber = $_POST['qqnumber'];
		$sex = $_POST['sex'];
		$height = $_POST['height'];
		$weight = $_POST['weight'];
		$birthday = $_POST['birthday'];
		$thumb = $_POST['thumb'];
		$description = $_POST['selfintro'];
		$resume = $_POST['resume'];
		$sporttitle = $_POST['sporttitle'];

		if($fullname==''){
			$return = array(
			    'result' => 3
			);
			ajax_return($return);
		}

		//修改或插入coach表
		$sql = "select * from coach where member_id=$member_id";
		$checksql = $this->dbmain->get_row($sql);

		if($checksql){
			$reviewcode = $checksql->review;
			$reviewarr = explode('|',$reviewcode);
			$reviewarr[0] = '1';
			$reviewcode = implode('|',$reviewarr);

		    $sql = "update coach set fullname='$fullname',height='$height',weight='$weight',birthday='$birthday',qqnumber='$qqnumber',sex='$sex',thumb='$thumb',description='$description',review='$reviewcode',resume='$resume',sporttitle='$sporttitle' where member_id=$member_id";
		    $this->dbmain->query($sql);
		}else{
			$sql = "insert into coach(fullname,height,weight,birthday,phonenumber,qqnumber,sex,createdate,member_id,thumb,description,resume,sporttitle) 
			values('$fullname','$height','$weight','$birthday','$phonenumber','$qqnumber','$sex',NOW(),$member_id,'$thumb','$description','$resume','$sporttitle')";
			$res = $this->dbmain->query($sql);
		}

	  	$return = array(
		    'result' => 1
		);
		ajax_return($return);

	} 

}
